package com.konsung.ksdc.manager.shop;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 取消订单页面
 * Created by DLX on 2016/5/31 0031.
 */
public class OrderCancelActivity extends BaseActivity {

    public static final String ORDER_ID = "orderId";
    @Bind(R.id.btn_confirm_cancel)
    Button btnConfirmCancel;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tabTvTitle;
    @Bind(R.id.rl_reason_container)
    RelativeLayout rlReasonContainer;
    @Bind(R.id.tv_save)
    TextView tvSave;
    @Bind(R.id.iv_counsel)
    ImageView ivCounsel;
    @Bind(R.id.rl_title_container)
    RelativeLayout rlTitleContainer;
    @Bind(R.id.et_reason)
    TextView tvReason;

    private String orderId;
    private PopupWindow popWindow;
    private String reason = "";

    @Override
    protected String activityDesc() {
        return getString(R.string.page_cancel_order);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancel_order);
        ButterKnife.bind(this);

        initView();
    }

    /**
     * 初始化取消订单页面
     */
    private void initView() {
        orderId = getIntent().getStringExtra(ORDER_ID);

        tabTvTitle.setText(R.string.title_cancel_order);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rlReasonContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View vPopWindow = View.inflate(MyApplication.getContext(),
                        R.layout.mypopwindow, null);

                popWindow = new PopupWindow(vPopWindow,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, true);

                ListView lvReason = (ListView) vPopWindow.findViewById(R.id
                        .lv_reason);
                // 将适配器set到ListView中去
                final List<String> reasonList = new ArrayList<String>();
                reasonList.add(getString(R.string.reason1));
                reasonList.add(getString(R.string.reason2));
                reasonList.add(getString(R.string.reason3));
                reasonList.add(getString(R.string.reason4));
                lvReason.setAdapter(new MenuItemAdapter(reasonList));
                lvReason.setOnItemClickListener(
                        new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent,
                                    View view, int position, long id) {
                                popWindow.dismiss();
                                reason = reasonList.get(position);
                                tvReason.setText(reason);
                            }
                        });

                popWindow.setBackgroundDrawable(new BitmapDrawable());
                popWindow.setOutsideTouchable(true);
                popWindow.showAsDropDown(v);
            }
        });

        btnConfirmCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String reason = tvReason.getText().toString();
                if (TextUtils.isEmpty(reason)) {
                    Toast.makeText(OrderCancelActivity.this, getString(R.string
                            .tip_cancel_reason_empty), Toast.LENGTH_SHORT).show();
                    return;
                }
                RequestUtils.cancleOrder(orderId, reason,
                        new RequestCallback() {
                            @Override
                            public void onError(Call call, Exception e) {
                            }

                            @Override
                            public void onResponse(ResponseBean responseBean) {
                                String resultCode = responseBean.
                                        getResultCode();
                                if (resultCode.equals(Constants.REQUEST_OK)) {
                                    Toast.makeText(OrderCancelActivity.this,
                                            R.string.tip_order_cancel_success,
                                            Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }
                        });
            }
        });
    }
}
